package cn.biplam.back.operator.systemManage.fileupdown;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Insert;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.SysManageCode.FileDwldOptCode;
import cn.biplam.common.struc.SysStruc.FileDownLoadStru;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 文件下载信息操作类
 * 表结构：
CREATE TABLE "FILE_DOWNLOAD" 
   (	"FILE_ID" VARCHAR2(20) NOT NULL ENABLE, 
	"FILE_THEME_ID" CHAR(1), 
	"FILE_NAME" CHAR(100), 
	"FILE_PATH" VARCHAR2(200), 
	"UPLOAD_USER" VARCHAR2(20), 
	"UPLOAD_DATE" VARCHAR2(10), 
	"UPLOAD_TIME" VARCHAR2(20), 
	 PRIMARY KEY ("FILE_ID"))
 * @author 	WangSong 2009-6-13 下午02:16:16
 * @version 1.0
 */
public class FileDownloadOpt extends DataOperator{
	/** 表名称 */
	private static final String tName="FILE_DOWNLOAD";
	private final String sKeyName="FILE_ID";
	private final String sThemeId="B";
	
	/**
	 * 创建系统序列管理表数据操作类
	 * @throws ErrorMessage
	 */
	public FileDownloadOpt() throws ErrorMessage{
		super(tName);
		super.mainKey=sKeyName;
	}

	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql=new ArrayList<String>();
		if(opId.equals(FileDwldOptCode.Query_ByID)){
			this.queryByID(sql, filter);
			return doing(sql, Operator_Type.Select);
		}else if(opId.equals(FileDwldOptCode.Insert_File)){
			this.insertFile(sql, filter);
			return doing(sql, Operator_Type.DML);
		}else{
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}
	
	/**
	 * 查询文件信息
	 * 参数: FILE_THEME_ID
	 */
	public void queryByID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {
		StringBuilder sb=new StringBuilder();
		sb.append("select * from ");
		sb.append(this.tableName);
		sb.append(" where FILE_ID='");
		sb.append(filter.getValue("id"));
		sb.append("'");
		sql.add(sb.toString());
	}
	
	/**
	 * 插入文件
	 */
	public void insertFile(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {
		Insert insert=new Insert(this.tableName);
		if(filter.getValue("file_id")==null){
			insert.putStringFieldIgnoreNull(sKeyName,this.generatorRowID());
		}else{
			insert.putStringFieldIgnoreNull(sKeyName,filter.getValue("file_id"));
		}
		insert.putStringFieldIgnoreNull("FILE_THEME_ID", sThemeId);
		insert.putStringFieldDenyNull(FileDownLoadStru.FILE_NAME, filter.getValue(FileDownLoadStru.FILE_NAME));	
		insert.putStringFieldDenyNull(FileDownLoadStru.FILE_PATH, filter.getValue(FileDownLoadStru.FILE_PATH));	
		insert.putStringFieldIgnoreNull(FileDownLoadStru.UPLOAD_USER, filter.getValue(FileDownLoadStru.UPLOAD_USER));	
		insert.putStringFieldIgnoreNull(FileDownLoadStru.UPLOAD_DATE, filter.getValue(FileDownLoadStru.UPLOAD_DATE));	
		insert.putStringFieldIgnoreNull(FileDownLoadStru.UPLOAD_TIME, filter.getValue(FileDownLoadStru.UPLOAD_TIME));
		sql.add(insert.getSimpleSql());
	}
}